package edu.zqu.collegeField.project.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.zqu.collegeField.common.utils.BeanMapper;
import edu.zqu.collegeField.framework.openfeign.api.GetCollegeNameApi;
import edu.zqu.collegeField.project.po.Field;
import edu.zqu.collegeField.project.po.FieldDepartmentExamine;
import edu.zqu.collegeField.project.mapper.FieldDepartmentExamineMapper;
import edu.zqu.collegeField.project.service.FieldDepartmentExamineService;
import edu.zqu.collegeField.project.vo.FieldDepartmentExamineVo;
import edu.zqu.collegeField.project.vo.FieldVo;
import edu.zqu.common.core.text.Convert;
import edu.zqu.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 场地申请部门审核结果 服务实现类
 * </p>
 *
 * @author 廖森
 * @since 2020-06-28
 */
@Service
public class FieldDepartmentExamineServiceImpl extends ServiceImpl<FieldDepartmentExamineMapper, FieldDepartmentExamine> implements FieldDepartmentExamineService {
    @Autowired
    private FieldDepartmentExamineMapper fieldDepartmentExamineMapper;
    @Autowired
    private GetCollegeNameApi getCollegeNameApi;

    @Override
    public List<FieldDepartmentExamineVo> get(BigInteger collegeId, //获取场地申请部门审核结果列表
                                              BigInteger fieldApplicationId,
                                              BigInteger departmentId) {
        HashMap<String, String> map = new HashMap<>();
        map.put("collegeId", Convert.toStr(collegeId));
        map.put("fieldApplicationId", Convert.toStr(fieldApplicationId));
        map.put("departmentId", Convert.toStr(departmentId));
        List<FieldDepartmentExamineVo> fieldDepartmentExamineVoList = BeanMapper.mapList(fieldDepartmentExamineMapper.get(map),
                FieldDepartmentExamineVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(collegeId);
        for (FieldDepartmentExamineVo fieldDepartmentExamineVo : fieldDepartmentExamineVoList) {
            fieldDepartmentExamineVo.setCollegeName(collegeName);
        }
        return fieldDepartmentExamineVoList;
    }

    @Override
    public FieldDepartmentExamineVo getById(BigInteger id) {//获取场地申请部门审核结果
        HashMap<String, String> map = new HashMap<>();
        map.put("id", Convert.toStr(id));
        FieldDepartmentExamineVo fieldDepartmentExamineVo = BeanMapper.map(fieldDepartmentExamineMapper.getById(map), FieldDepartmentExamineVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(fieldDepartmentExamineVo.getCollegeId());
        fieldDepartmentExamineVo.setCollegeName(collegeName);
        return fieldDepartmentExamineVo;
    }

    @Override
    public int add(FieldDepartmentExamine fieldDepartmentExamine) {//添加场地申请部门审核结果
        fieldDepartmentExamine.setId(null).setDeleted(null).setUpdateTime(new Timestamp(System.currentTimeMillis()));
        return fieldDepartmentExamineMapper.insert(fieldDepartmentExamine);
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int update(FieldDepartmentExamine fieldDepartmentExamineUpdate) {//修改场地申请部门审核结果
        FieldDepartmentExamine fieldDepartmentExamine = fieldDepartmentExamineMapper.selectById(fieldDepartmentExamineUpdate.getId());
        if (StringUtils.isNull(fieldDepartmentExamine)) {
            throw new NullPointerException("找不到该部门审核结果");
        }
        BeanMapper.copyPropertiesNotNull(fieldDepartmentExamineUpdate, fieldDepartmentExamine);
        try {
            int insert = fieldDepartmentExamineMapper.insert(fieldDepartmentExamine.setLastVersion(fieldDepartmentExamineUpdate.getId()));
            if (insert >= 1) {
                return delete(fieldDepartmentExamineUpdate.getId(), fieldDepartmentExamineUpdate.getByWho());
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("更新失败");
        }
        return 0;
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int delete(BigInteger id, BigInteger byWho) {//删除场地申请部门审核结果
        try {
            UpdateWrapper<FieldDepartmentExamine> fieldDepartmentExamineUpdateWrapper = new UpdateWrapper<FieldDepartmentExamine>().eq("id", Convert.toStr(id));
            int update = fieldDepartmentExamineMapper.update(new FieldDepartmentExamine().setByWho(byWho), fieldDepartmentExamineUpdateWrapper);
            if (update >= 1) {
                return fieldDepartmentExamineMapper.delete(fieldDepartmentExamineUpdateWrapper);
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("删除失败");
        }
        return 0;
    }
}
